package top.chj.day17.exam.test;

import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

/**
 * @Auther:小陈的Java
 */


public class User2{
    private int id;
    private String name;
    private int age;

    public User2(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public User2() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        User2[] users = new User2[3];
        users[0] = new User2(1,"李四",17);
        users[1] = new User2(1,"王五",18);
        users[2] = new User2(1,"张三",19);
        Arrays.sort(users, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1==null || o2==null){
                    throw  new RuntimeException("null不支持排序");
                }
                if(!(o1 instanceof User2 && o2 instanceof User2)){
                    throw  new ClassCastException("非User类型不能排序");
                }
                User2 user1 = (User2)o1;
                User2 user2 = (User2)o2;
                if(user1.getAge()!=user2.getAge()){
                    return user1.getAge()-user2.getAge();
                } else {
                    Collator collator = Collator.getInstance(Locale.CHINA);
                    return  collator.compare(user1.getName(),user2.getName());
                }
            }
        });
        System.out.println(Arrays.toString(users));
    }


}

